﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using ReagentStore.Properties;
using ReagentStore.UTL;

namespace ReagentStore
{
	public partial class AccountForm : Form
	{
		public AccountForm()
		{
			InitializeComponent();
			buttonOK.Enabled = false;
		}

		private void textOldPWD_Validating(object sender, CancelEventArgs e)
		{
			string pwd = textOldPWD.Text;

			if (string.IsNullOrEmpty(pwd)
			    || (!DESEncryptor.Encrypt(pwd).Equals(Settings.Default.Password)))
			{
				errorProvider.SetError(textOldPWD, "密码输入错误");
				e.Cancel = true;
				return;
			}
			else
			{
				errorProvider.SetError(textOldPWD, string.Empty);
				textNewPWD.Focus();
				buttonOK.Enabled = true;
			}
		}

		private void textRepeatPWD_Validating(object sender, CancelEventArgs e)
		{
			string pwd1 = textNewPWD.Text;
			string pwd2 = textRepeatPWD.Text;

			if (!pwd1.Equals(pwd2)
			    || string.IsNullOrEmpty(pwd1)
			    || pwd1.Length < 6
			    || string.IsNullOrEmpty(pwd2)
			    || pwd2.Length < 6)
			{
				errorProvider.SetError(textRepeatPWD, "密码应至少为6位");
				e.Cancel = true;
				return;
			}
			else
				errorProvider.SetError(textRepeatPWD, string.Empty);
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			Settings.Default.Password = DESEncryptor.Encrypt(textNewPWD.Text);
			Settings.Default.Save();

			MessageBox.Show("密码更换成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
		}

		private void textNewPWD_Validating(object sender, CancelEventArgs e)
		{
			string pwd1 = textNewPWD.Text;

			if (string.IsNullOrEmpty(pwd1)
			    || (pwd1.Length < 6))
			{
				errorProvider.SetError(textNewPWD, "密码应至少为6位");
				e.Cancel = true;
				return;
			}
			else
				errorProvider.SetError(textNewPWD, string.Empty);
		}
	}
}